﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TugasAkhir.Models;
using TugasAkhir.Objects;

namespace TugasAkhir
{
    public partial class KuisionerIsi : System.Web.UI.Page
    {
        private MahasiswaObject mahasiswa;
        private DataSet dsKuisioner;
        private DataSet dsJawaban;
        private Dictionary<string, int> jawaban;

        protected void Page_Load(object sender, EventArgs e)
        {
            CheckSession();
            if (Request.QueryString["kelas"] != null && Request.QueryString["periode"] != null && Request.QueryString["tahun"] != null && Request.QueryString["matakuliah"] != null && Request.QueryString["mahasiswa"] != null)
            {
                this.InitJawaban();
                this.InitKuisioner();
            }
            else
            {
                Response.Redirect("/Kuisioner.aspx", true);
            }
        }

        private void InitKuisioner()
        {
            if (DetailPertanyaanModel.GetPertanyaanKuisioner(Request.QueryString["mahasiswa"], Request.QueryString["kelas"], Request.QueryString["tahun"], Request.QueryString["matakuliah"], Request.QueryString["periode"], out this.dsKuisioner))
            {
                if (dsKuisioner.Tables[0] != null && dsKuisioner.Tables[0].Rows.Count > 0)
                {
                    int count = 0;
                    this.jawaban = new Dictionary<string, int>();

                    foreach (DataRow item in dsKuisioner.Tables[0].Rows)
                    {
                        var isiPertanyaan = new Literal
                        {
                            ID = item[0].ToString(),
                            Text = ++count + ".&nbsp;" + item[1].ToString() + "<br/>"
                        };

                        this.jawaban[item[0].ToString()] = 0;

                        this.pnlPertanyaan.Controls.Add(isiPertanyaan);

                        for (int i = 0; i < 4; i++)
                        {
                            var radio = new RadioButton {
                                ID = String.Format("{0}_{1}", item[0], i),
                                Text=this.dsJawaban.Tables[0].Rows[i][1].ToString(),
                                CssClass = "radio",
                                GroupName = item[0].ToString()
                            };

                            radio.Attributes["value"] = (i + 1).ToString();
                            this.pnlPertanyaan.Controls.Add(radio);
                        }
                    }

                    var submit = new Button
                    {
                        ID = "btSubmitJawaban",
                        Text = "Submit",
                        CssClass = "btn btn-default"
                    };

                    submit.Click += (s, e) =>
                    {
                        this.ValidateKuisioner();
                    };

                    this.pnlPertanyaan.Controls.Add(submit);
                }
            }
        }

        private void ValidateKuisioner()
        {
            foreach (var control in this.pnlPertanyaan.Controls)
            {
                if (control is RadioButton)
                {
                    if ((control as RadioButton).Checked)
                    {
                        this.jawaban[(control as RadioButton).GroupName] = int.Parse((control as RadioButton).Attributes["value"]);
                    }
                }
            }

            bool flag = true;
            foreach (var item in this.jawaban)
            {
                if (item.Value == 0)
                    flag = false;
            }

            if (flag)
            {
                foreach (var item in this.jawaban)
                {
                    DetailPertanyaanModel.Update(Request.QueryString["mahasiswa"], Request.QueryString["kelas"], Request.QueryString["tahun"], Request.QueryString["matakuliah"], Request.QueryString["periode"], item.Value.ToString(), item.Key);
                }
                Response.Redirect("/Kuisioner.aspx", true);
            }
        }

        private void InitJawaban()
        {
            DetailPertanyaanModel.GetJawaban(out this.dsJawaban);
        }

        private void CheckSession()
        {
            if (Session["id_role"] != null && Session["id_role"].ToString() == "RL01")
            {
                MahasiswaModel.GetMahasiswaDetail(Session["username"].ToString(), out mahasiswa);
                this.litName.Text = mahasiswa.Nama;
            }
            else
            {
                Response.Redirect("/Login.aspx");
            }
        }

        protected void lnkLogout_Click(object sender, EventArgs e)
        {
            Session["id_role"] = "";
            Session["username"] = "";
            CheckSession();
        }
    }
}